namespace EastenCommunication.Core.IMessage
{
	/// <summary>
	/// 西门子Fetch/Write消息解析协议
	/// </summary>
	public class FetchWriteMessage : INetMessage
	{
		/// <inheritdoc cref="P:EastenCommunication.Core.IMessage.INetMessage.ProtocolHeadBytesLength" />
		public int ProtocolHeadBytesLength => 16;

		/// <inheritdoc cref="P:EastenCommunication.Core.IMessage.INetMessage.HeadBytes" />
		public byte[] HeadBytes
		{
			get;
			set;
		}

		/// <inheritdoc cref="P:EastenCommunication.Core.IMessage.INetMessage.ContentBytes" />
		public byte[] ContentBytes
		{
			get;
			set;
		}

		/// <inheritdoc cref="P:EastenCommunication.Core.IMessage.INetMessage.SendBytes" />
		public byte[] SendBytes
		{
			get;
			set;
		}

		/// <inheritdoc cref="M:EastenCommunication.Core.IMessage.INetMessage.GetContentLengthByHeadBytes" />
		public int GetContentLengthByHeadBytes()
		{
			if (SendBytes != null)
			{
				if (HeadBytes[5] == 4)
				{
					return 0;
				}
				return SendBytes[12] * 256 + SendBytes[13];
			}
			return 16;
		}

		/// <inheritdoc cref="M:EastenCommunication.Core.IMessage.INetMessage.CheckHeadBytesLegal(System.Byte[])" />
		public bool CheckHeadBytesLegal(byte[] token)
		{
			if (HeadBytes == null)
			{
				return false;
			}
			if (HeadBytes[0] == 83 && HeadBytes[1] == 53)
			{
				return true;
			}
			return false;
		}

		/// <inheritdoc cref="M:EastenCommunication.Core.IMessage.INetMessage.GetHeadBytesIdentity" />
		public int GetHeadBytesIdentity()
		{
			return HeadBytes[3];
		}
	}
}
